/**
 * <p>Title: ServiceZMCS31.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-25 上午10:21:26  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.zm.cs.domain.ZMCSInitialDataDetails;

/**隶属于初始数据模块：初始数据明细
 * <p>Title: ServiceZMCSInitialDataDetails.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 路涛
 * @version 1.0
 * <pre>History:
 *          2010-12-17 下午01:33:45  路涛  Create
 *</pre>
 */
public class ServiceZMCS31 extends ServiceEPBase {
	
	/**
	 * 初始化列表数据方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-24下午08:44:45 严洪浩
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "100");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		String runType = FrameworkInfo.getProjectType();
		outInfo.set("runType", runType);// 运行状态

		return outInfo;
	}
	
	/**
	 * 查询方法
	 * 根据查询条件查询从表数据信息
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialDataDetails</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:36:45  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdInitialDataId = inInfo.getString("fdInitialDataId");
		StaticTools.setQueryParameters(inInfo, "fdInitialDataId", fdInitialDataId, true);// 添加查询条件

		EiInfo outInfo = inInfo;
		if (fdInitialDataId != null)
			outInfo = super.query(inInfo, "ZMCSInitialDataDetails.query", new ZMCSInitialDataDetails());
		else
			outInfo.addBlock("result").setBlockMeta(new ZMCSInitialDataDetails().eiMetadata);

		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.type", new String[] { "系统", "用户" }, new String[] { "系统", "用户" }, false));// 系统类型
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cs.status", new String[] { "停用", "在用" }, new String[] { "停用", "在用" }, false));// 状态
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * 新增从表数据
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialDataDetails</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:38:45  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		EiInfo out=new EiInfo(); 
		ZMCSInitialDataDetails _dom = new ZMCSInitialDataDetails();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			boolean validate = validate(inInfo, _dom, "ZMCSInitialDataDetails.validate", params);
			if(validate) {
				return inInfo;
			}
			else {
				String fdInitialDataId = inInfo.getString("fdInitialDataId");
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_INITIALDATADETAILS"));//主键
				_dom.setFdInitialDataId(fdInitialDataId);
				this.dao.insert("ZMCSInitialDataDetails.insert",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
		
		
		
		
//		//校验名称、简称是否重复
//		boolean validate = validate(inInfo, "ZMCSInitialDataDetails.validate", new HashMap<String, String>());
//		
//		if(validate) {
//			return inInfo;
//		}
//		else {
//			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_INITIALDATADETAILS"));//主键
//			String fdInitialDataId = inInfo.getString("fdInitialDataId");
//			
//			inInfo.getBlock("result").setCell(0, "fdInitialDataId", fdInitialDataId);
//			EiInfo outInfo = super.insert(inInfo, "ZMCSInitialDataDetails.insert");
//			changeInitialData(outInfo);//如果修改成功则重新刷新数据
//			return outInfo;
//		}
	}
	
	/**
	 * 修改方法
	 * 修改从表数据
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialDataDetails</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:43:45  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		EiInfo out=new EiInfo(); 
		ZMCSInitialDataDetails _dom = new ZMCSInitialDataDetails();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = validate(inInfo, _dom, "ZMCSInitialDataDetails.validate", params);
			if(validate) {
				return inInfo;
			}
			else {
				String fdInitialDataId = inInfo.getString("fdInitialDataId");
				_dom.setFdInitialDataId(fdInitialDataId);
				_dom.setFdCreatorId( UserSession.getUserId().toString());//默认为当前登录用户
				this.dao.update("ZMCSInitialDataDetails.update",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
		
		
		
		
//		//校验名称、简称是否重复
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
//		
//		boolean validate = validate(inInfo, "ZMCSInitialDataDetails.validate", params);
//		if(validate) {
//			return inInfo;
//		}
//		else {
//			String fdInitialDataId = inInfo.getString("fdInitialDataId");
//			inInfo.getBlock("result").setCell(0, "fdInitialDataId", fdInitialDataId);
//			EiInfo outInfo = super.update(inInfo, "ZMCSInitialDataDetails.update");
//			changeInitialData(outInfo);//如果修改成功则重新刷新数据
//			return outInfo;
//		}
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSInitialDataDetails</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2010-12-17 下午01:33:45  路涛  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo _temp = super.delete(inInfo, "ZMCSInitialDataDetails.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        
        changeInitialData(outInfo);//如果修改成功则重新刷新数据
		return outInfo;
	}
	
	/**
	 * 数据明细表的状态为启用状态
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-25下午04:51:27 路涛
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCSInitialDataDetails.modifyStart");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        
        changeInitialData(outInfo);//如果修改成功则重新刷新数据
		return outInfo;
	}
	
	/**
	 * 数据明细表的状态为停用状态
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-25下午04:55:27 路涛
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSInitialDataDetails.modifyStop");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        
        changeInitialData(outInfo);//如果修改成功则重新刷新数据
		return outInfo;
	}
	
	/**
	 * 在新增、修改操作时校验名称序号简称不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:33:45  路涛  Create
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCSInitialDataDetails _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdName = _dom.getFdName();
		String fdInitialDataId = _dom.getFdInitialDataId();
		String fdSerialNumber = _dom.getFdSerialNumber().toString();
		
		params.put("fdName", fdName);
		params.put("fdInitialDataId", fdInitialDataId);
		params.put("fdSerialNumber", fdSerialNumber);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSInitialDataDetails _temp = (ZMCSInitialDataDetails)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			if(_temp.getFdSerialNumber() != null && _temp.getFdSerialNumber().toString().equals(fdSerialNumber)) {
				msg.append(" 序号 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 如果修改成功则刷新数据
	 * <p>Function: changeInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午02:59:02 严洪浩
	 * </p>
	 *
	 */
	private void changeInitialData(EiInfo outInfo){
		if(outInfo != null && outInfo.getStatus() != -1) {
			CmesConstants.INITIALDATA_STATUS = 1;
		}
	}

}
